<template>
    <view>
        <!-- pages/setVip/setVip.wxml -->
        <view class="search">
            <van-search v-model="name" placeholder="请输入昵称、手机号" use-action-slot>
                <view slot="action" @tap="search">搜索</view>
            </van-search>
            <view class="sorts">
                <view class="sort border-primary color-primary" @tap="search" data-info="createTime">
                    <text>注册时间</text>
                    <view class="iconfont icon-paixu"></view>
                </view>
                <view class="sort border-primary color-primary" @tap="search" data-info="orderTime">
                    <text>下单时间</text>
                    <view class="iconfont icon-paixu"></view>
                </view>
                <view class="sort border-primary color-primary" @tap="search" data-info="orderCount">
                    <text>订单数量</text>
                    <view class="iconfont icon-paixu"></view>
                </view>
            </view>
        </view>
        <view class="container">
            <block v-if="MainList.length">
                <!-- 列表 -->
                <view class="list">
                    <view class="item" v-for="(item, index) in MainList" :key="index">
                        <view class="top">
                            <view class="left">
                                <view class="img">
                                    <image :src="item.avatar ? item.avatar : '/logo.png'" mode="widthFix"></image>
                                </view>
                                <view class="nick">{{ item.nickname }}</view>
                                <view></view>
                            </view>
                            <view class="right">
                                <view class="info">
                                    手机号 ：{{ item.mobile }}
                                    <text class="link color-primary" v-if="!isSelect" @tap="copy" :data-info="item.mobile">点击复制</text>
                                </view>
                                <view class="info">注册时间：{{ item.createTime }}</view>
                                <view class="info">最近下单：{{ item.orderTime ? item.orderTime : '' }}</view>
                                <view class="info">
                                    总订单数：
                                    <text>{{ item.orderCount }}</text>
                                    单
                                </view>
                                <view class="info" v-if="!isSelect">
                                    账户余额：
                                    <text>{{ item.balance }}</text>
                                    元
                                </view>
                                <view class="info" v-if="!isSelect">
                                    赠送余额：
                                    <text>{{ item.giftBalance }}</text>
                                    元
                                </view>
                            </view>
                        </view>

                        <!-- 会员管理 -->

                        <view class="btns">
                            <button class="btn red" @tap="recharge" :data-info="item">余额充值</button>
                            <navigator class="btn red" :url="'../setCoupon/setCoupon?isSelect=1&&userId=' + item.id">赠送优惠券</navigator>
                            <navigator class="btn red" :url="'../SetOrder/SetOrder?userId=' + item.id">查看订单</navigator>
                        </view>
                    </view>
                </view>
                <view v-if="canLoadMore" class="noteMore">下拉刷新查看更多...</view>
            </block>
            <block v-else>
                <view class="nodata">暂无数据</view>
            </block>
        </view>

        <!-- <van-dialog use-slot title="余额充值" show="{{ showRecharge }}" show-cancel-button cancel-button-text="取消" confirm-button-text="确认" bind:confirm="confirmRecharge">
  <view class="dialog">
    <view class="item">
      <label>用户昵称：</label>
      <text>{{member.nickname}} </text>
    </view>
    <view class="item">
      <label>手机号码：</label>
      <text>{{member.mobile}} </text>
    </view>
    <view class="item">
      <label>充值金额：</label>
      <input model:value="{{mobile}}" type="digit" placeholder="请输入充值金额" />
    </view>
    <view class="tip">
      充值金额输入0，代表清空余额
    </view>
  </view>
</van-dialog> -->

        <!-- 余额充值 -->
        <van-dialog use-slot title="余额充值" :show="showRecharge" show-cancel-button @confirm="confirmRecharge" @cancel="cancelRecharge">
            <view class="dialog">
                <van-cell-group>
                    <van-field label="门店：" placeholder="请选择门店">
                        <picker slot="input" @change="bindStoreChange" :value="index" range-key="text" :range="stores">
                            <view class="picker">
                                <input type="text" disabled placeholder="请选择门店" placeholder-class="plh" :value="stores[index].text" />
                                <van-icon name="arrow-down" />
                            </view>
                        </picker>
                    </van-field>
                    <van-field v-model="member.nickname" label="用户昵称：" />
                    <van-field v-model="member.mobile" label="手机号：" />
                    <van-field v-model="money" tyoe="number" label="充值金额：" placeholder="请输入大于0的数字" :border="false" />
                    <view class="tip">金额输入0等于清空该会员余额</view>
                </van-cell-group>
            </view>
        </van-dialog>
    </view>
</template>

<script>
// pages/setVip/setVip.js
const app = getApp();
var http = require('../../../utils/http');
var util1 = require('../../../utils/util.js');
export default {
    data() {
        return {
            isSelect: 0,
            MainList: [],

            //列表数组
            stores: [],

            index: '',
            canLoadMore: true,

            //是否还能加载更多
            pageNo: 1,

            pageSize: 10,
            name: '',
            cloumnName: 'orderTime',
            sortRule: 'DESC',
            createRule: 'ASC',
            orderRule: 'ASC',
            numRule: 'ASC',
            couponId: '',
            showRecharge: false,

            member: {
                nickname: '',
                mobile: ''
            },

            money: '',
            storeId: '',
            text: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        //console.log(options)
        let isSelect = parseInt(options.isSelect);
        let couponId = parseInt(options.couponId);
        this.setData({
            isSelect: isSelect ? isSelect : '',
            couponId: couponId ? couponId : ''
        });
        if (isSelect === 1) {
            uni.setNavigationBarTitle({
                title: '选择会员'
            });
        }
        this.getListData('refresh');
        this.getXiaLaListAdmin();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this;
        this.setData({
            MainList: [],
            //列表数组
            canLoadMore: true,
            //是否还能加载更多
            pageNo: 1
        });
        this.getListData('refresh');
        uni.stopPullDownRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let that = this;
        if (that.canLoadMore) {
            that.pageNo++;
            this.getListData('');
        } else {
            uni.showToast({
                title: '我是有底线的...'
            });
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        //获取列表数据
        getListData: function (e) {
            var that = this;
            let message = '';
            if (app.globalData.isLogin) {
                if (e == 'refresh') {
                    //刷新，page变为1
                    message = '正在加载';
                    that.setData({
                        pageNo: 1
                    });
                }
                http.request(
                    '/member/manager/getMemberPage',
                    '1',
                    'post',
                    {
                        pageNo: that.pageNo,
                        pageSize: that.pageSize,
                        name: that.name,
                        cloumnName: that.cloumnName,
                        sortRule: that.sortRule
                    },
                    app.globalData.userDatatoken.accessToken,
                    message,
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            if (e == 'refresh') {
                                that.setData({
                                    MainList: info.data.list
                                });
                                if (info.data.list.length === 0) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                            } else {
                                if (info.data != null && info.data.list.length <= info.data.total) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                                let arr = that.MainList;
                                let arrs = arr.concat(info.data.list);
                                that.setData({
                                    MainList: arrs
                                });
                            }
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            } else {
                uni.showModal({
                    content: '请您先登录，再重试！',
                    showCancel: false
                });
            }
        },

        // 搜索
        search: function (e) {
            let cloumnName = e.currentTarget.dataset.info;
            if (cloumnName) {
                let sortRule = '';
                if (cloumnName == 'createTime') {
                    sortRule = this.createRule == 'ASC' ? 'DESC' : 'ASC';
                    this.setData({
                        createRule: this.createRule == 'ASC' ? 'DESC' : 'ASC'
                    });
                } else if (cloumnName == 'orderTime') {
                    sortRule = this.orderRule == 'ASC' ? 'DESC' : 'ASC';
                    this.setData({
                        orderRule: this.orderRule == 'ASC' ? 'DESC' : 'ASC'
                    });
                } else if (cloumnName == 'orderCount') {
                    sortRule = this.numRule == 'ASC' ? 'DESC' : 'ASC';
                    this.setData({
                        numRule: this.numRule == 'ASC' ? 'DESC' : 'ASC'
                    });
                }
                this.setData({
                    cloumnName: cloumnName,
                    sortRule: sortRule
                });
            }
            this.getListData('refresh');
        },

        // 赠送优惠券
        select: function (e) {
            let userId = e.currentTarget.dataset.info;
            console.log('userId' + userId);
            uni.showModal({
                title: '提示',
                content: '是否确定赠送该会员',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        var that = this;
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/manager/giftCoupon',
                                '1',
                                'post',
                                {
                                    couponId: that.couponId,
                                    userId: userId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '赠送成功'
                                        });
                                        setTimeout(() => {
                                            uni.navigateBack();
                                        }, 1000);
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        // 点击复制
        copy: function (e) {
            let data = e.currentTarget.dataset.info;
            var that = this;
            uni.setClipboardData({
                data: data,
                success(res) {
                    uni.showToast({
                        title: '复制成功'
                    });
                }
            });
        },

        recharge: function (e) {
            let data = e.currentTarget.dataset.info;
            this.setData({
                member: data,
                money: '',
                storeId: '',
                index: '',
                showRecharge: true
            });
        },

        confirmRecharge: function (e) {
            var that = this;
            if (that.storeId) {
                if (app.globalData.isLogin) {
                    http.request(
                        '/member/manager/recharge',
                        '1',
                        'post',
                        {
                            userId: that.member.id,
                            storeId: that.storeId,
                            money: that.money
                        },
                        app.globalData.userDatatoken.accessToken,
                        '提交中',
                        function success(info) {
                            console.info('返回111===');
                            console.info(info);
                            if (info.code == 0) {
                                uni.showToast({
                                    title: '充值成功'
                                });
                            } else {
                                uni.showModal({
                                    content: info.msg,
                                    showCancel: false
                                });
                            }
                        },
                        function fail(info) {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    );
                }
            } else {
                uni.showToast({
                    title: '未选择门店'
                });
            }
        },

        cancelRecharge: function (e) {
            this.setData({
                member: '',
                money: '',
                storeId: '',
                showRecharge: false
            });
        },

        //管理员获取门店下拉列表数据
        getXiaLaListAdmin: function (e) {
            var that = this;
            //if (app.globalData.isLogin)
            {
                http.request(
                    '/member/store/getStoreList',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('下拉门店数据===');
                        console.info(info);
                        if (info.code == 0) {
                            let stores = [];
                            info.data.map((it) => {
                                stores.push({
                                    text: it.key,
                                    value: it.value
                                });
                            });
                            that.setData({
                                stores: stores
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        bindStoreChange: function (e) {
            this.setData({
                index: e.detail.value,
                storeId: this.stores[e.detail.value].value
            });
        }
    }
};
</script>
<style>
@import './setVip.css';
</style>
